/*
 *  Copyright 2007 Genoworks
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License. 
 */

package com.genoworks.retriever.bigdir;

class BigFileSegment implements Comparable<BigFileSegment> {

  private int source = 0;

  private long offset = 0L;

  private long length = 0;

  BigFileSegment( int src, long position, long len ) {
    source = src;
    offset = position;
    length = len;
  }

  int getSource() {
    return source;
  }

  long getOffset() {
    return offset;
  }

  long getLength() {
    return length;
  }

  public int compareTo( BigFileSegment seg ) {
    if ( source < seg.source ) {
      return -1;
    } else if ( source > seg.source ) {
      return 1;
    } else if ( offset < seg.offset ) {
      return -1;
    } else if ( offset > seg.offset ) {
      return 1;
    }
    return 0;
  }

  @SuppressWarnings( "nls" )
  String toDirString() {
    return source + ":" + offset + ":" + length;
  }

  @SuppressWarnings( "nls" )
  static BigFileSegment fromDirString( String str ) {
    String[] strArray = str.split( ":" );
    if ( strArray.length != 3 ) {
      throw new IllegalArgumentException( "" );
    }
    int source = Integer.parseInt( strArray[ 0 ] );
    long offset = Long.parseLong( strArray[ 1 ] );
    int length = Integer.parseInt( strArray[ 2 ] );
    return new BigFileSegment( source, offset, length );
  }

  public String toString() {
    return "BigFileSegment{dataId=" + source
        + ",offset="
        + offset
        + ",length="
        + length
        + "}";
  }
}
